package com.rapida.anim;

import android.view.animation.Interpolator;

public class ValueArrayInterpolator implements Interpolator {
	private final static String TAG = "ValueArrayInterpolator";
	private float[] mValues = null;
	private int mValueCount = 0;

	public void setValueArray(float[] values) {
		if (values == null) {
			return;
		}
		mValues = values.clone();
		mValueCount = mValues.length;
	}

	@Override
	public float getInterpolation(float arg0) {
		float ret = 0.0f;
		if (mValues == null) {
			ret = 1.0f;
		} else if (mValues.length <= 0) {
			ret = 1.0f;
		} else if (mValues.length == 1) {
			ret = mValues[0];
		} else if (arg0 <= 0.0f) {
			ret = mValues[0];
		} else if (arg0 >= 1.0f) {
			ret = mValues[mValueCount - 1];
		} else {
			float segment = 1.0f / (float) (mValueCount - 1);
			int index = (int) (arg0 / segment);
			float extra = arg0 - segment * (float) index;
			ret = mValues[index] + (mValues[index + 1] - mValues[index]) * extra / segment;
		}
		return ret;
	}
}
